var pingtai
var text;
var content_management_id = getQueryString("content_management_id")
var CangName=""
var code=""
var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

$.each(warehouses.data,function(k,s){
    code +=s.warehouseCode+ ","
    CangName+=s.warehouseName+"/"
})
//去掉最后一个逗号(如果不需要去掉，就不用写)
if (code.length > 0) {
    code = code.substr(0, code.length - 1);
}
if (CangName.length > 0) {
    CangName = CangName.substr(0, CangName.length - 1);
}
$(function () {
    var userData={
        contentManagementId: getQueryString("content_management_id"),
        nowTime: new Date().Format("yyyy-MM-dd hh:mm:ss")
    }
    $.ajax({
        type: "post",
        url: root.sroreinter.search_content_conf,
        cache: false,  //禁用缓存
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(userData),
        //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data[0].richText;
                ue.ready(function() {
                    ue.setContent(dataInter);
                });
                $("#title").val(data.data[0].title);
                $("#logmin").val(data.data[0].startTime);
                $("#logmax").val(data.data[0].endTime);
                $("#sequence").val(data.data[0].displaySequence);
                $("#ordinary_url").val(data.data[0].ordinaryUrl);
                $("#contract_url").val(data.data[0].contractUrl);
                code=data.data[0].warehouseCode;
                code=code.split(',')
                var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
                for(var i=0;i<code.length;i++){//id相同为选中状态
                    console.log(code[i])
                    if(code.indexOf(code[i])>= 0){
                        $.each(warehouses.data,function(k,s){
                            if(code[i]==s.warehouseCode){
                                var warecode= document.getElementById('ying'+s.warehouseCode+'')
                                warecode.checked=true
                            }
                        })
                    }
                }
                if(code.indexOf("49") >= 0 ) {
                    $("#jiangsu").attr("checked","checked")
                }
                if(code.indexOf("50") >= 0 ) {
                    $("#wuhan").attr("checked","checked")
                }
                code=data.data[0].warehouse_code;
                CangName=data.data[0].warehouse_name;
                pingtai=data.data[0].platform;
                if (pingtai=='10'){
                    $("#qianyue").attr("checked","checked")
                }else if (pingtai=='20'){
                    $("#putong").attr("checked","checked")
                }else if (pingtai=='10,20'){
                    $("#putong").attr("checked","checked")
                    $("#qianyue").attr("checked","checked")
                }
            }
        }
    })
})

$("#form-member").validate({
    rules: {
        sequence:"sequenceCheck"
    },
    //提交表单数据
    submitHandler:function(form){
        //循环class，获取到每个check的选中状态
        if (pingtai=='0'){
            $("#putong").css('outline','1px solid red')
            $("#qianyue").css('outline','1px solid red')
            layer.msg("请配置平台", {icon: 6, time: 3000});
            return false
        }
        if (code==''){
            var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
            $.each(warehouses.data,function(k,s){
                var warecode= document.getElementById('ying'+s.warehouseCode+'')
                $(warecode).css('outline','1px solid red');
            })
            layer.msg("请配置区域", {icon: 6, time: 3000});
            return false
        }
        ue.ready(function() {
            text = ue.getContent();
        });
        //提交
        var user_data = {
            content_management_id : content_management_id,
            platform : pingtai,
            warehouse_code : code,
            warehouse_name : CangName,
            display_sequence : $("#sequence").val(),
            title : $("#title").val(),
            start_time : $("#logmin").val(),
            end_time : $("#logmax").val(),
            ordinary_url: $("#ordinary_url").val(),
            contract_url: $("#contract_url").val(),
            rich_text:text,
            last_modified_name:JSON.parse(web.readCookie("loginData")).user.username,
            last_modified_by:JSON.parse(web.readCookie("loginData")).user.user_ID
        };
        //获取返回的数据
        var succCallBack = function (data, status, response) {
            console.log(data);
            if(data.errorCode=="0") {
                layer.msg('操作成功!', {icon: 6, time: 1000});
                parent.isEdit = true;
                parent.queryDataFn();
                setTimeout( function () {
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                },700);
            }
            if (data.errorCode=="93"){
                layer.msg('开始时间不能大于结束时间,请重新查看', {icon: 6, time: 3000});
                return
            }
        }
        //给一个接口地址提交数据
        return web.doAjax(root.sroreinter.save_content_conf, 'post', user_data, succCallBack);
    }
})


function changeCang() {
    code=''
    CangName=''
    var warcode=""//id
    var warcodename=""//name
    var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
    $.each(warehouses.data,function(k,s){
        /* var uu= document.getElementById('ying'+s.warehouseCode+'')
          uu.css('outline','')*/
        if(document.getElementById('ying'+s.warehouseCode+'').checked==true){//获取选中的id
            warcode+=document.getElementById('ying'+s.warehouseCode+'').value+ ","//逗号拼接获取到的所以id
            warcodename+=document.getElementById('ying'+s.warehouseCode+'').name+ "/"
        }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    if (warcodename.length > 0) {
        warcodename = warcodename.substr(0, warcodename.length - 1);
    }
    code=warcode
    CangName=warcodename
}
function changePingtai() {
    $("#putong").css('outline','')
    $("#qianyue").css('outline','')
    if ($("#qianyue").is(":checked")&&$("#putong").is(":checked")){
        pingtai='10'+","+'20'
    }
    if ($("#qianyue").is(":checked")&&!$("#putong").is(":checked")){
        pingtai='10'
    }
    if ($("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        pingtai='20'
    }
    if (!$("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        pingtai='0'
    }
}
function removeIframe(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


